import 'package:get/get.dart';
import '../../model/member_model.dart';
import '../../model/user_data.dart';
import '../../request/api.dart';
import '../../request/requestManager.dart';
import '../../res/r.dart';
import '../../utils/sp_utils.dart';

class MemberController extends GetxController {
  UserData? userData;
  MemberModel? memberModel;
  List<MemberList> goldList = [];
  List<MemberPrivileges> goldPrivilegeList = [];
  List<MemberList> diamondList = [];
  List<MemberPrivileges> diamondPrivilegeList = [];
  List<String> paymentList = [];
  int typeIndex = 0;

  @override
  void onInit() {
    super.onInit();
    getLocalUserData();
    getVipData();
  }

  void selectTypeIndex(int index) {
    typeIndex = index;
    update();
  }

  void getLocalUserData() async {
    // userData = SpUtil.getUserData();
    // update();
    var result = await RequestManager().post(API.myInfo);
    if (result != null && result['code'] == 0) {
      UserData user = UserData.fromJson(result['data']);
      SpUtil.saveUserData(user);
      userData = user;
      update();
    }
  }

  String setMemberDate() {
    if (memberModel == null) return '未开通';
    if (memberModel?.info?.on == 0) {
      return memberModel?.info?.expireDate ?? '未开通';
    } else {
      if (userData?.userinfo?.vip == 0) {
        if (userData?.userinfo?.vipLevel == 3) {
          // 黄金永久会员
          return "已开通黄金永久会员";
        } else if (userData?.userinfo?.vipLevel == 2) {
          // 黄金季度会员
          return "已开通黄金季度会员,${memberModel?.info?.expireDate}";
        } else if (userData?.userinfo?.vipLevel == 1) {
          // 黄金单月会员
          return "已开通黄金单月会员,${memberModel?.info?.expireDate}";
        }
      } else if (userData?.userinfo?.vip == 1) {
        if (userData?.userinfo?.vipLevel == 3) {
          // 钻石永久会员
          return "已开通钻石永久会员";
        } else if (userData?.userinfo?.vipLevel == 2) {
          // 钻石季度会员
          return "已开通钻石季度会员,${memberModel?.info?.expireDate}";
        } else if (userData?.userinfo?.vipLevel == 1) {
          // 钻石单月会员
          return "已开通钻石单月会员,${memberModel?.info?.expireDate}";
        }
      }
    }
    return '';
  }

  void getVipData() async {
    var result = await RequestManager().post(API.getMemberData);
    if (result != null && result['code'] == 0) {
      memberModel = MemberModel.fromJson(result['data']);
      memberModel?.info?.list?.forEach((element) {
        if (element.memberType == 0) {
          goldList.add(element);
        } else if (element.memberType == 1) {
          diamondList.add(element);
        }
      });
      memberModel?.privileges?.forEach((element) {
        if (element.memberType == 0) {
          goldPrivilegeList.add(element);
        } else if (element.memberType == 1) {
          diamondPrivilegeList.add(element);
        }
      });
      memberModel?.channels?.forEach((element) {
        if (element.channelName != null) {
          paymentList.add(element.channelName!);
        }
      });
      update();
    }
  }

  String setGoldVipIcon(int index) {
    if (typeIndex == 0) {
      if (index == 0) {
        return ImagePath.assetsImagesGoldVip1;
      } else if (index == 1) {
        return ImagePath.assetsImagesGoldVip2;
      } else {
        return ImagePath.assetsImagesGoldVip3;
      }
    } else {
      if (index == 0) {
        return ImagePath.assetsImagesDiamondVip1;
      } else if (index == 1) {
        return ImagePath.assetsImagesDiamondVip2;
      } else {
        return ImagePath.assetsImagesDiamondVip3;
      }
    }
  }
}
